import json
import os.path

from login_func import login,register,inputcash,withdraw,transfer


filename = "user_info.txt"
if os.path.isfile(filename):
    with open(filename) as fp:
        user_info = json.load(fp)

else:
    user_info= {}


while True:
    choice = input("1、登录\n2、注册\n3、退出\n请输入你的选择:")
    match choice:
        case '1':
            print("欢迎进入登录页面!")
            for i in range(3):
                name = input("please input your username(press q quit):")
                if name == 'q':
                    break
                password = input("please input your password:")
                if login(name,password,user_info):
                    print("登录成功")
                    while True:
                        choice = input("a、存钱\nb、取钱\nc、转账\nd、退出\n请输入你的选择:")
                        match choice:
                            case 'a':
                                money = int(input("请输入你需要存入的金额:"))
                                inputcash(name,user_info,money)
                            case 'b':
                                money = int(input("请输入你需要取出的金额:"))
                                withdraw(name,user_info,money)
                            case 'c':
                                people = input("请输入你需要转入的账户名:")
                                money = int(input("请输入你需要转出的金额:"))
                                transfer(name,people,user_info,money)
                            case 'd':
                                break
                            case  _:
                                print("输入选项有误请重新输入")
                    break
                else:
                    print(f"账号密码错误，你还剩{2-i}次机会！")


        case '2':
            print("欢迎进入注册页面")
            name = input("please input your username(press q quit):")
            if name == 'q':
                continue
            password = input("please input your password:")
            balance = int(input("please input your balance:"))
            if register(name, password,balance,user_info):
                print("注册成功！")
            else:
                print("注册失败！")

        case '3':
            break

        case  _:
            print('请输入正确的选项！')

with open(filename,'w') as fp:
    json.dump(user_info,fp)